package Slots;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 *
 * @author Ziad Aloulou
 */
public class Start extends JPanel implements ActionListener {

    private Console console;
    private Graphics2D g2d;
    public int vitesse = 0;
    public int vitesseInitiale = vitesse;
    public int additionVitesse = 15;
    public int addition = 1;
    public int intervalle = 200;
    public boolean slots = true;
    public boolean slots2 = true;
    public Timer timerDefilement;
    public Timer timerVitesse;
    //coordonnées
    public int y1 = 299;
    public int y2 = 351;
    public int y3 = 403;
    public int y1a = y1;
    public int y2a = y2;
    public int y3a = y3;
    public int y1b = y1;
    public int y2b = y2;
    public int y3b = y3;
    public int xd1 = 140;
    public int xf1 = 208;
    public int xd3 = 241;
    public int xf3 = 309;
    public int xd5 = 341;
    public int xf5 = 409;
    //
    public int i1;
    public int i2;
    public int i3;
    public int j1;
    public int j2;
    public int j3;
    //
    public Image IMAGE_SLOTS1 = Toolkit.getDefaultToolkit().createImage("Images/Slots1.JPG");
    public Image IMAGE_SLOTS2 = Toolkit.getDefaultToolkit().createImage("Images/Slots2.JPG");
    //
    public Image IMAGE_FRAISE = Toolkit.getDefaultToolkit().createImage("Images/Fraise.JPG");
    public Image IMAGE_CERISE = Toolkit.getDefaultToolkit().createImage("Images/Cerise.JPG");
    public Image IMAGE_RAISIN = Toolkit.getDefaultToolkit().createImage("Images/Raisin.JPG");
    public Image IMAGE_ORANGE = Toolkit.getDefaultToolkit().createImage("Images/Orange.JPG");
    public Image IMAGE_WATERMELON = Toolkit.getDefaultToolkit().createImage("Images/Watermelon.JPG");
    public Image IMAGE_PRUNE = Toolkit.getDefaultToolkit().createImage("Images/Prune.JPG");
    //
    public Matrice matrice;

    public Start(Console console) {
        this.console = console;
        this.setOpaque(false);
        matrice = new Matrice();
        timerDefilement = new Timer(vitesse, this);
        timerVitesse = new Timer(intervalle, this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (vitesse < vitesseInitiale + additionVitesse * 3) {
            if (e.getSource() == timerDefilement) {
                if (y1 >= 404) {
                    y1 = 248;
                    new Sound("SE/button.wav");
                }
                if (y2 >= 404) {
                    y2 = 248;
                    new Sound("SE/button.wav");
                }
                if (y3 >= 404) {
                    y3 = 248;
                    new Sound("SE/button.wav");
                }
                if (y1a >= 404) {
                    y1a = 248;
                }
                if (y2a >= 404) {
                    y2a = 248;
                }
                if (y3a >= 404) {
                    y3a = 248;
                }
                if (y1b >= 404) {
                    y1b = 248;
                }
                if (y2b >= 404) {
                    y2b = 248;
                }
                if (y3b >= 404) {
                    y3b = 248;
                }
                y1 = y1 + addition;
                y2 = y2 + addition;
                y3 = y3 + addition;
                y1a = y1a + addition;
                y2a = y2a + addition;
                y3a = y3a + addition;
                y1b = y1b + addition;
                y2b = y2b + addition;
                y3b = y3b + addition;
                for (int j = 0; j < matrice.getLengthY(); j++) {
                    for (int i = 0; i < matrice.getLengthX(); i++) {
                        matrice.matriceSymbole[i][j].setPositionY(matrice.matriceSymbole[i][j].getPositionY() + addition);
                    }
                }
                repaint();
            } else if (e.getSource() == timerVitesse) {
                timerDefilement.stop();
                vitesse += additionVitesse;
                intervalle -= 750;
                timerDefilement = new Timer(vitesse, this);
                timerDefilement.start();
            }
        } else {
            if (y1 != 403) {
                if (y1 >= 404) {
                    y1 = 248;
                    new Sound("SE/button.wav");
                }
                if (y2 >= 404) {
                    y2 = 248;
                    new Sound("SE/button.wav");
                }
                if (y3 >= 404) {
                    y3 = 248;
                    new Sound("SE/button.wav");
                }
                if (y1a >= 404) {
                    y1a = 248;
                }
                if (y2a >= 404) {
                    y2a = 248;
                }
                if (y3a >= 404) {
                    y3a = 248;
                }
                if (y1b >= 404) {
                    y1b = 248;
                }
                if (y2b >= 404) {
                    y2b = 248;
                }
                if (y3b >= 404) {
                    y3b = 248;
                }
                y1 = y1 + addition;
                y2 = y2 + addition;
                y3 = y3 + addition;
                y1a = y1a + addition;
                y2a = y2a + addition;
                y3a = y3a + addition;
                y1b = y1b + addition;
                y2b = y2b + addition;
                y3b = y3b + addition;
                for (int j = 0; j < matrice.getLengthY(); j++) {
                    for (int i = 0; i < matrice.getLengthX(); i++) {
                        matrice.matriceSymbole[i][j].setPositionY(matrice.matriceSymbole[i][j].getPositionY() + addition);
                    }
                }
            } else if (y1 == 403 && slots == true) {
                if (y1a >= 404) {
                    y1a = 248;
                    new Sound("SE/button.wav");
                }
                if (y2a >= 404) {
                    y2a = 248;
                    new Sound("SE/button.wav");
                }
                if (y3a >= 404) {
                    y3a = 248;
                    new Sound("SE/button.wav");
                }
                if (y1b >= 404) {
                    y1b = 248;
                }
                if (y2b >= 404) {
                    y2b = 248;
                }
                if (y3b >= 404) {
                    y3b = 248;
                }
                y1a = y1a + addition;
                y2a = y2a + addition;
                y3a = y3a + addition;
                y1b = y1b + addition;
                y2b = y2b + addition;
                y3b = y3b + addition;
                for (int j = 0; j < matrice.getLengthY(); j++) {
                    for (int i = 1; i < matrice.getLengthX(); i++) {
                        matrice.matriceSymbole[i][j].setPositionY(matrice.matriceSymbole[i][j].getPositionY() + addition);
                    }
                }
                if (y1 == 403 && y2a == 403 && y2b == 403) {
                    slots = false;
                }
            } else if (y1 == 403 && slots == false && slots2 == true) {
                if (y1b >= 404) {
                    y1b = 248;
                    new Sound("SE/button.wav");
                } else if (y2b >= 404) {
                    y2b = 248;
                    new Sound("SE/button.wav");
                } else if (y3b >= 404) {
                    y3b = 248;
                    new Sound("SE/button.wav");
                }
                y1b = y1b + addition;
                y2b = y2b + addition;
                y3b = y3b + addition;
                for (int j = 0; j < matrice.getLengthY(); j++) {
                    for (int i = 2; i < matrice.getLengthX(); i++) {
                        matrice.matriceSymbole[i][j].setPositionY(matrice.matriceSymbole[i][j].getPositionY() + addition);
                    }
                }
                if (y1 == 403 && y2a == 403 && y3b == 403) {
                    slots2 = false;
                }
            } else if (y1 == 403 && slots == false && slots2 == false) {
                for (int j = 0; j < matrice.getLengthY(); j++) {
                    for (int i = 0; i < matrice.getLengthX(); i++) {
                        if (matrice.matriceSymbole[i][j].getPositionX() == 140 && matrice.matriceSymbole[i][j].getPositionY() == 300) {
                            i1 = i;
                            j1 = j;
                        }
                        if (matrice.matriceSymbole[i][j].getPositionX() == 241 && matrice.matriceSymbole[i][j].getPositionY() == 300) {
                            i2 = i;
                            j2 = j;
                        }
                        if (matrice.matriceSymbole[i][j].getPositionX() == 342 && matrice.matriceSymbole[i][j].getPositionY() == 300) {
                            i3 = i;
                            j3 = j;
                        }

                    }

                }
                if (matrice.matriceSymbole[i1][j1].getSymbole() == matrice.matriceSymbole[i2][j2].getSymbole() && matrice.matriceSymbole[i1][j1].getSymbole() == matrice.matriceSymbole[i3][j3].getSymbole()) {
                    Display display = new Display(console);
                    display.setMessage("YOU WIN MOFUCKA!");
                    console.add(new Display(console));
                } else {
                    Display display = new Display(console);
                    display.setMessage("YOU LOSE NIGGA!");
                    console.add(new Display(console));
                }
                console.btnPlay.setEnabled(true);
            }

        }
        repaint();
    }

    @Override
    public void paintComponent(Graphics g) {

        g2d = (Graphics2D) g;
        g2d.setColor(Color.BLACK);
        g2d.drawLine(xd1, y1, xf1, y1);
        g2d.drawLine(xd1, y2, xf1, y2);
        g2d.drawLine(xd1, y3, xf1, y3);
        g2d.drawLine(xd3, y1a, xf3, y1a);
        g2d.drawLine(xd3, y2a, xf3, y2a);
        g2d.drawLine(xd3, y3a, xf3, y3a);
        g2d.drawLine(xd5, y1b, xf5, y1b);
        g2d.drawLine(xd5, y2b, xf5, y2b);
        g2d.drawLine(xd5, y3b, xf5, y3b);

        for (int j = 0; j < matrice.getLengthY(); j++) {
            for (int i = 0; i < matrice.getLengthX(); i++) {
                if (matrice.matriceSymbole[i][j] != null) {
                    if (matrice.matriceSymbole[i][j].getPositionY() < 189 || matrice.matriceSymbole[i][j].getPositionY() > 402) {
                    } else {
                        if (matrice.matriceSymbole[i][j].getSymbole() == 1) {
                            g2d.drawImage(IMAGE_RAISIN, matrice.matriceSymbole[i][j].getPositionX(), matrice.matriceSymbole[i][j].getPositionY(), null);
                        } else if (matrice.matriceSymbole[i][j].getSymbole() == 2) {
                            g2d.drawImage(IMAGE_CERISE, matrice.matriceSymbole[i][j].getPositionX(), matrice.matriceSymbole[i][j].getPositionY(), null);
                        } else if (matrice.matriceSymbole[i][j].getSymbole() == 3) {
                            g2d.drawImage(IMAGE_FRAISE, matrice.matriceSymbole[i][j].getPositionX(), matrice.matriceSymbole[i][j].getPositionY(), null);
                        } else if (matrice.matriceSymbole[i][j].getSymbole() == 4) {
                            g2d.drawImage(IMAGE_ORANGE, matrice.matriceSymbole[i][j].getPositionX(), matrice.matriceSymbole[i][j].getPositionY(), null);
                        } else if (matrice.matriceSymbole[i][j].getSymbole() == 5) {
                            g2d.drawImage(IMAGE_WATERMELON, matrice.matriceSymbole[i][j].getPositionX(), matrice.matriceSymbole[i][j].getPositionY(), null);
                        } else if (matrice.matriceSymbole[i][j].getSymbole() == 6) {
                            g2d.drawImage(IMAGE_PRUNE, matrice.matriceSymbole[i][j].getPositionX(), matrice.matriceSymbole[i][j].getPositionY(), null);
                        }
                    }
                    g2d.drawImage(IMAGE_SLOTS1, 132, 0, null);
                    g2d.drawImage(IMAGE_SLOTS2, 132, 404, null);

                    if (matrice.matriceSymbole[i][j].getPositionY() >= 560) {
                        matrice.matriceSymbole[i][j].setPositionY(92);

                    }
                }
            }
        }
    }
}
